package cn.tansanqinger.controller.hy_menu;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.tansanqinger.pojo.HyMenu;
import cn.tansanqinger.service.hy_menu.IHyMenuService;
import cn.tansanqinger.util.Page;
import cn.tansanqinger.util.PageData;
import cn.tansanqinger.util.Tools;

@Controller
@RequestMapping("/admin/umenu")
public class HyMenuController {

	@Autowired
	private IHyMenuService service;
	
	@RequestMapping("/list")
	public ModelAndView list(ModelAndView mv) {
		mv.addObject("firstMenu", service.selectFirstMenus());
		mv.setViewName("hy_menu/list");
		return mv;
	}
	
	/**
	 * 查询数据，进行分页
	 * @param request
	 * @return
	 */
	@RequestMapping("/findListData")
	@ResponseBody
	public String findListData(HttpServletRequest request) {
		try {
			PageData pd = new PageData(request);
			String keywords = pd.getString("keywords");	//关键词检索条件
			if(null != keywords && !"".equals(keywords)){
				pd.put("keywords", keywords.trim());
			}
			pd.put("data", service.selectHyMenus(pd));
			pd.put("count", service.selectHyMenuLength(pd));
			pd.put("code","0");
			pd.put("msg", "查询成功");
			return new ObjectMapper().writeValueAsString(pd);
		} catch (Exception e) {}
		return null;
	}
	
	/**
	 * 前往添加数据页面
	 * @param mv
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goAdd")
	public ModelAndView goAdd(ModelAndView mv) throws Exception {
		mv.setViewName("hy_menu/goAdd");
		mv.addObject("firstMenu",service.selectFirstMenus());
		service.selectSnMax();
		return mv;
		
	}
	
	/**
	 * 添加数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/addMenu")
	public @ResponseBody PageData addMenu(HttpServletRequest request) {
		PageData pageData = new PageData(request);
		return service.addMenu(pageData);
	} 
	
	/**
	 * 查询对应菜单的下一个编号
	 * @param request
	 * @return
	 */
	@RequestMapping("/serialNumber")
	public @ResponseBody int serialNumber(HttpServletRequest request) {
		PageData pageData = new PageData(request);
		return service.serialNumber(pageData);
	}
	
	/**
	 * 菜单编辑跳转页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request,ModelAndView mv) {
		PageData pageData = new PageData(request);
		HyMenu hymenu = service.selectMenuIdOne(pageData);
		mv.addObject("menu", hymenu);
		if(hymenu.getHparentId()==0) {
			mv.addObject("menuClassify","菜单");
		} else {
			pageData.put("hmenuId", hymenu.getHparentId());
			mv.addObject("menuClassify", service.selectMenuIdOne(pageData).getHmenuName());
		}
		mv.addObject("firstMenu", service.selectFirstMenus());
		mv.setViewName("hy_menu/edit");
		return mv;
	}
	
	/**
	 * 修改菜单数据
	 * @param request
	 * @param mv
	 * @return
	 */
	@RequestMapping("/modification")
	public @ResponseBody PageData modification(HttpServletRequest request) {
		PageData pageData = new PageData(request);
		return service.updataMenu(pageData);
	}
	
	/**
	 * 修改菜单状态
	 * @param request
	 * @return
	 */
	@RequestMapping("/updateMenuStatus")
	public @ResponseBody PageData updateMenuStatus(HttpServletRequest request) {
		PageData pageData = new PageData(request);
		return service.updateMenuStatus(pageData);
	}
	
	/**
	 * 删除数据
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete")
	public @ResponseBody PageData delete(HttpServletRequest request) {
		PageData pageData = new PageData(request);
		return service.deleteMenu(pageData);
	}
}
